/******************************************************************************
/// @brief
/// @copyright Copyright (c) 2017 <dji-innovations, Corp. RM Dept.>
/// @license MIT License
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction,including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense,and/or sell
/// copies of the Software, and to permit persons to whom the Software is furnished
/// to do so,subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
*******************************************************************************/


#include "kb.h"
#include "bsp_uart.h"
#include "chassis_task.h"
#include "cmsis_os.h"
#include "mytype.h"

/************************* global vars *******************************/
u16          key_code          = 0;
vu8          kb_move_mode      = 0;
const u16    max_kb_move_speed = 200;
/************************* global vars *******************************/

enum
{
    KEY_IDLE = 0, // no key action, idle sta
    KEY_DOWN_WAIT_CONFIRM, // maybe in key shake sta, need to confirm according to
    // next time.
    KEY_DOWN_WAIT_RELEASE, // after key release, a full key press action is done.
} key_sta_e;

// used for avoid mouse  press shake
u8 key_fsm(u8* psta, u8 condition)
{
    u8 ret = 0; // temp var must init! or it is a random
    switch (*psta)
    {
    case KEY_IDLE:
        if (condition)
            *psta = KEY_DOWN_WAIT_CONFIRM;
        break;

    case KEY_DOWN_WAIT_CONFIRM:
        if (condition)
        {
            *psta = KEY_DOWN_WAIT_RELEASE;
            ret   = 1; // triggered when key is not release yet.
        }
        else
            *psta = KEY_IDLE;
        break;

    case KEY_DOWN_WAIT_RELEASE:
        if (!condition)
            *psta = KEY_IDLE;
        break;
    }
    return ret;
}

/**
*@public:  in the future, delete kb.c this file/task, combine to chassis.DONE
**/
void pc_kb_hook(float* vx,float* vy)
{
    //    if(key_code & SHIFT)
    //      kb_move_mode = ShiftQuicklyMode;
    //    else if(key_code & CTRL)
    //      kb_move_mode = CtrlSlowlyMode;
    //    else
    //      kb_move_mode = NormalMode;

    // WSAD move
    if (rc.kb.bit.W) // added to max slowly
        *vy += 4;
    else if (rc.kb.bit.S)
        *vy += -4;
    else
        *vy = 0;

    if (rc.kb.bit.A)
        *vx += -4;
    else if (rc.kb.bit.D)
        *vx += 4;
    else
        *vx = 0;


    VAL_LIMIT(*vx, -max_kb_move_speed, max_kb_move_speed);
    VAL_LIMIT(*vy, -max_kb_move_speed, max_kb_move_speed);
}
